package org.friping.mytool.views;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.friping.mytool.R;
import org.friping.mytool.helper.JsonHelper;

import java.util.List;
import java.util.Map;

import org.friping.mytool.helper.ActivityHelper;

@EActivity(R.layout.a_main)
public class MainActivity extends AppCompatActivity {

    @ViewById
    Toolbar toolbar;

    @ViewById
    ListView listView;

    SimpleAdapter listViewAdapter;

    @Bean
    ActivityHelper activityHelper;
    @Bean
    JsonHelper jsonHelper;

    @AfterViews
    public void afterView(){
        setSupportActionBar(toolbar);
        initListView();
    }

    @ItemClick
    public void listViewItemClicked(int position){
        Map itemObj = (Map) listViewAdapter.getItem(position);
        String id = (String) itemObj.get("id");
        if("UNIT_SEARCH".equals(id)){
            this.startActivity(activityHelper.buildIntent(this, UnitSearchActivity_.class));
        }else if("UNIT_EDIT".equals(id)){
            this.startActivity(activityHelper.buildIntent(this, UnitEditActivity_.class));
        }else if("UNIT_STAT".equals(id)){
            this.startActivity(activityHelper.buildIntent(this, UnitStatActivity_.class));
        }
    }

    private void initListView() {
        List<Map<String, String>> items = jsonHelper.readListFromAssets(this, "main_screen_items.json");
        listViewAdapter = new SimpleAdapter(this, items, R.layout.it_simple, new String[]{"name"}, new int[]{R.id.title});
        listView.setAdapter(listViewAdapter);
    }
}
